<?php
class game{

	private $statut = 0;
	private $nb_player = 0;
	private $players = array();
	private $current_player = 0;
	private $de_pige;
	private $de1;
	private $de2;
	//accesseurs
	function __get($name) {
    		return $this->$name;
  	}
  	function __set($name,$value) {
    		$this->$name=$value;
  	}
  	private function reset_game(){
  		$this->statut = 0;
  		$this->nb_player = 0;
  		$this->players = array();
  		$this->current_player = 0;
  	}
	private function next_step(){
		$this->statut++;	
	}
	private function next_player(){
		$this->current_player++;
	}
	private function add_player($player){	
		$this->players[] = $player;
	}
	private function remove_player($player){
		unset($his->players[$player->id]);		
	}
	private function start(){
		$this->de_pige = new des(0);
		$this->de1 = new des(1);
		$this->de2 = new des(2);
		foreach ($this->players as $player){
			$player->game_stat["nb_coup_bu"] = 0;
			$player->game_stat["nb_coup_donne"] = 0;
			$player->game_stat["nb_coup_des"] = 0;
		}
		$this->next_step();
	}
	private function end(){
		$this->update_player_stat();
	}
	private function update_player_stat(){
		foreach ($this->players as $player){
			$player->update_stat();
		}
	}
	public function draw(){
		$this->get_game();
		switch ($this->statut){
		case 0:
			$grahp = new graph($this);
			$this->next_step();
			break;
		case 1:	
			$this->get_player_number();
			$grahp = new graph($this);
			$this->next_step();
			break;
		case 2:			
			$this->get_players();
			$grahp = new graph($this);
			$this->start();
			break;
		case 3:			
			$graph = new graph($this);
			$this->play();
			echo "</div></div>";
			break;
		case 4:
			$grahp = new graph($this);				
			$this->play();
			echo "</div></div>";
			break;
		case 5:
			echo "<h2> fin du tour </h2>";
			//$game->show_players_game_stat();
			$grahp = new graph($this);
			$this->end();
			break;
		}
	}

	private function play(){
		$player = $this->players[$this->current_player];
		echo '<div class="result_area">';
		echo "<h3>Joueur ".($player->id+1)." : ".$player->pseudo."</h3>";
		if ($this->statut == 3){
		//Tirage des Piges
			//tirage du dés 
			$this->de_pige->shot();
			//simple pigeon	
			if ($this->de_pige->value == 3){
				$player->pigeon = 1;				
				print "t'est pigeon<br/>";			
			}
			//multi pigeon
			elseif ($this->de_pige->value == 6){
				$player->pigeon = 2;
				$this->de_pige->shot();
				while ($this->de_pige->value == 6){				
					$player->pigeon ++;				
					$this->de_pige->shot();
					print "multi pigeon ?<br/>";
				}	
				if ($this->de_pige->value == 3){
					print " Youhou t'est ".$player->pigeon."X pigeon <br/>Bonne chance<br/>";
				}
				else{
					$player->pigeon = 0;
					print " Dommage<br/>";
				}
			}

			if ( $this->current_player == $this->nb_player-1){
				$this->current_player = 0;
				foreach ($this->players as $player){
					if ($player->pigeon > 0){
						$this->statut = 4;
						echo 	"<form METHOD='POST' action='index_new.php'>\n			
						<input type=submit value='Début du jeu'/>\n
						</form>\n";
					}			
				}
				if ($this->statut != 4){
					echo 	"<form METHOD='POST' action='index_new.php'>\n			
					<input type=submit value='Tour suivant'/>\n
					</form>\n";
				}
			}
			else{
				$this->next_player();						
				echo 	"<form METHOD='POST' action='index_new.php'>\n			
				<input type=submit value='Joueur suivant'/>\n
				</form>\n";
			}
		}
		elseif($this->statut == 4){
		//Jeu du Pigeon
			//Tirage des dés
			$this->de1->shot();
			$this->de2->shot();
			$de1 = $this->de1->value;
			$de2 = $this->de2->value;
			//moteur du jeu
			$player->game_stat['nb_coup_des']++;
			$action =false;
			if ($de1== 3 and $de2==3){
				$action = true;
				echo "Les pigeons boivent 2 fois plus! <br />";
				$this->faire_boire($this->get_players_id($player,"pigeon"));
				$this->faire_boire($this->get_players_id($player,"pigeon"));
			}
			else if ($de1== 3 or $de2==3){
				$action = true;
				echo "Les pigeons boivent ! <br />";
				$this->faire_boire($this->get_players_id($player,"pigeon"));
			}
			if (($de1+$de2) == 9){
				$action = true;
				echo "Let's go ! </br>";
			}
			if (($de1==5 and $de2==1) or ($de2 == 5 and $de1 == 1)){
				$action = true;
				echo "51 Tous le monde bois !!!! <br />";
				$this->faire_boire($this->get_players_id($player,"all"));
			}
			if (($de1+$de2) == 7){
				$action = true;
				$this->faire_boire($this->get_players_id($player,"droite"));
			  	echo "A droite tu bois !";
			}
			if (($de1+$de2) == 11){
				$action = true;
				$this->faire_boire($this->get_players_id($player,"gauche"));
				echo "A gauche tu bois !";
			}
			if ($de1 == $de2){
				$action = true;
				echo "Double " . $de1 . ", Tu distribus ".$de1. " coups !! <br/>";
			}
			
			if ($action == false){
				if ($player->game_stat['nb_coup_des']==1){
					echo "Rien tu bois";
					$player->bois();
				}
				else{
					echo "Rien";
				}
				if ( $this->current_player == $this->nb_player-1){		
					$this->statut = 5;
					echo "<form name='game' METHOD='POST' action='index_new.php'>\n			
						<input type=submit value='Coup suivant' onClick=\"submit_doubles(".$de1.",".$de2.");\"'/>\n
					</form>\n";		
				}
				else{
					$this->current_player++;
					echo "<form name='game' METHOD='POST' action='index_new.php'>\n			
						<input type=submit value='Coup suivant' onClick=\"submit_doubles(".$de1.",".$de2.");\"'/>\n
						</form>\n";
				}
			}
			else{
				echo "<form name='game' METHOD='POST' action='index_new.php'>\n			
					<input type=submit value='Coup suivant' onClick=\"submit_doubles(".$de1.",".$de2.");\"'/>\n
					</form>\n";
			}
		}
		echo '</div>';
	}

	private function faire_boire($id_players){
		foreach ( $id_players as $id_player ){
			$this->players[$id_player]->bois();
			print $this->players[$id_player]->pseudo." bois"."<br/>";
		}
	}
	private function get_players_id($player,$mode){
		$return_array = array();
		switch ($mode){
			case ("droite"):
				if (($player->id - 1) < 0){
					$return_array[] = $this->nb_player - 1;
				}
				else{
					$return_array[] = $player->id - 1;
				}
				break;
			case ("gauche"):
				if (($player->id + 1) >= $this->nb_player){
					$return_array[] = 0;
				}
				else{
					$return_array[] = $player->id + 1;
				}
				break;
			case ("all"):
				foreach($this->players as $key => $player_temp){
					if ($key != $player->id){
						$return_array[] = $key;
					}
				}
				break;
			case ("pigeon"):
				foreach($this->players as $key => $player_temp){
					$i=0;
					while ($i < $player_temp->pigeon){
						$return_array[] = $key;
						$i++;
					}
				}
				break;	
		}
		$this->players[$player->id]->game_stat['nb_coup_donne'] = $this->players[$player->id]->game_stat['nb_coup_donne'] + count($return_array);
		return $return_array;		
	}
	//recuperation de données
	private function get_player_number(){
		$this->nb_player = $_POST['nbjoueur'];
		if($this->nb_player < 3){
			$this->reset_game();
		}
	}
	private function get_players(){
		$i=0;
		while ($_POST["name_j".$i]){
			$this->add_player(new player(htmlspecialchars($_POST["name_j".$i]),$i));
			$i++;
		}
	}
	private function get_game(){
		if ($_POST["game"] == "new"){
			$this->reset_game();
		}
		elseif ($_POST["game"] == "next"){
			$this->statut = 3;
  			$this->current_player = 0;
  			foreach ($this->players as $player){
  				$player->pigeon=-1;
  			}
		}
	}

	private function save_game($name){
		if ($this->check_base_name($name) == false){
			$players_save = array();
			foreach ($this->players as $player){
				$players_save[$player->id]['pseudo']= $player->pseudo;
				$players_save[$player->id]['game_stat']= $player->game_stat;
				$players_save[$player->id]['pigeon']= $player->pigeon;
			}
				mysql_query("INSERT INTO game (name,statut,nb_player,players,current_player) VALUES ('".$name."',".$this->statut.",".$this->nb_player.",'".serialize($players_save)."',".$this->current_player.")");
		}
		else{
		//rentré le retour erreur
		}
	}
	
	private function load_game($name){
		$players = array();
		$query=mysql_query("SELECT statut,nb_player,players,current_player from game where name='".$name."'") or die ('Erreur : '.mysql_error());
		while ($loadgame = mysql_fetch_array($query)){ 
			$arrayplayers = unserialize($loadgame['players']);
			$this->statut = $loadgame['statut'];
			$this->nb_player = $loadgame['nb_player'];
			$this->current_player = $loadgame['current_player'];
		}
		foreach ($arrayplayers as $key => $item){
			$player=new player($item['pseudo'],$key);
			$player->game_stat = $item['game_stat'];
			$player->pigeon = $item['pigeon'];
			$player->load_stat();
			$this->add_player($player);
		}
	}
	
	private function check_base_name($name){
		return mysql_num_rows(mysql_query("SELECT * FROM game WHERE name = '".$name."'"));	
	}
}
class des extends game{
	public $value = 0;
	private $images = array();
	private $id = 0;
	//constructeur
	function __construct($id) {
		$this->id = $id;
		$this->load_images();
	}
	
	function __get($name) {
    		return $this->$name;
  	}
  	function __set($name,$value) {
    		$this->$name=$value;
  	}
  	
	protected function load_images(){
		if (count($this->images) > 0 ){
			$this->images = array();
		}
		for($i=1; $i <= 6; $i++){
		$this->images[$i] = $i.".jpg";
		}
	}
	protected function shot(){
		$this->value = rand(1,6);
		$this->affiche();
	}
	private function affiche(){
		if ($this->id == 2){
			echo "<div name=\"de\" id='de2' class=\"des\" style=\"background-image: url('images/".$this->images[$this->value]."');left : 350px;\">";
		}
		else{
		echo "<div name=\"de\" class=\"des\" style=\"background-image: url('images/".$this->images[$this->value]."');\">";
		}
		
		echo $this->value."<br/>";
		echo "</div>";
	}
}
?>
